﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcCarConsume.Domain.Entites;

namespace MvcCarConsume.Domain
{
    public class SqlCartService : ICartService
    {
        private string connStr;

        public SqlCartService(string connectionString)
        {
            connStr = connectionString;
        }

        //public Car CarWithID(int id)
        //{
        //    using (var db = new MvcCarDBEntities())
        //    {
        //        return db.Car.FirstOrDefault(p => p.BrandId == id);
        //    }
        //}
        /// <summary>
        /// 验证用户输入的数量是否合法 true合法 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public bool CheckNumOfCars(int id, int count)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
                int CurrentCount = db.Car.Single(a => a.BrandId == id).Count;
                if (count > CurrentCount)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public bool SaveCartInfo(int id, int count, int days, string starttime, string UserName)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
               // int CurrentCount = db.Car.Single(a => a.BrandId == id).Count;
                var CarInfo = db.Car.FirstOrDefault(a => a.CarId == id);
                if (CarInfo != null)
                {
                    CarInfo.Count = CarInfo.Count - count;
                    db.SaveChanges();

                    Order o = new Order();
                    o.UserName = UserName;
                    o.CarId = CarInfo.CarId;
                    o.Count = count;
                    o.BuildDate = DateTime.Now;
                    o.BorrowDate = Convert.ToDateTime(starttime);
                    o.SendDate = o.BorrowDate.AddDays(days);
                    o.Price = CarInfo.Price * count * days;
                    o.State = 0;
                    db.AddToOrder(o);
                    db.SaveChanges();
                }
                else
                {
                    return false;
                }           
                
            }
            return true;
        }
        
        public Order[] GetOrdersByUsername(string Username)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
                var Orders = db.Order.Where(o => o.UserName == Username);
                if (Orders != null)
                    return Orders.ToArray();
                else
                    return null;
            }
        }


        public bool Pay4OrderById(int id)
        {
            using (var db =new MvcCarDBEntities(connStr))
            {
                var Order = db.Order.Single(o => o.OrderId == id);
                if (Order != null)
                {
                    Order.State = 1;
                    db.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        public bool Pay()
        {
            throw new NotImplementedException();
        }

        public bool ReturnOrderById(int id)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
                var Order = db.Order.Single(o => o.OrderId == id);
                if (Order != null)
                {
                    Order.State = 3;

                    var car = db.Car.FirstOrDefault(c => c.CarId == Order.CarId);
                    car.Count += Order.Count;

                    db.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        public bool ReturnOrder()
        {
            throw new NotImplementedException();
        }

        public bool EditOrderById(int id)
        {
            throw new NotImplementedException();
        }


        public bool cancel(int id)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
                var order = db.Order.Single(o => o.OrderId == id);
                if (order != null)
                {
                    var car = db.Car.Single(c => c.CarId == order.CarId);
                    car.Count += order.Count;

                    db.Order.DeleteObject(order);
                    db.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }
    }
}
